package com.teamsun.mqtt.service;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.teamsun.mqtt.entity.vo.UserVo;

import io.netty.handler.codec.mqtt.MqttConnectReturnCode;

/**
 * 
 * @author acer
 * 
 */
public class UserPwdValidate {


	Logger logger=Logger.getLogger("server");
	

	static Map<String, String> userpwds = new HashMap<String, String>();
	

	boolean init;

	public void init() {

		if (!init) {
			userpwds.put("admin", "admin123456");
			userpwds.put("user", "user123456");
					
	}

	}
	
	

	public UserPwdValidate(
			) {
		super();
	}

	/**
	 * 验证用户名密码 返回一个验证结果
	 * @param validate
	 * @return
	 */
	public  MqttConnectReturnCode validateUserLogin(UserVo validate){


		MqttConnectReturnCode returnCode=MqttConnectReturnCode.CONNECTION_REFUSED_NOT_AUTHORIZED;
		
			String key = validate.getUsername();
			if (userpwds.containsKey(key)) {
				String pwd = userpwds.get(key);
				if (pwd.equals(validate.getPassword())){
					returnCode=MqttConnectReturnCode.CONNECTION_ACCEPTED;
				}
				else{
					returnCode=MqttConnectReturnCode.CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD;
					
					if(logger.isInfoEnabled()){
						logger.info(key+"用户名验证失败");
					}
				}

			}else{
				returnCode=MqttConnectReturnCode.CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD;
			}

			
			return returnCode;

	}
}
